package com.syncshot.androidapp;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;
import com.syncshot.androidapp.activities.TakePictureActivity;
import com.syncshot.androidapp.helper.NotificationHelper;
import com.syncshot.androidapp.utils.*;


public class GCMIntentService extends GCMBaseIntentService {

	@Override
	protected void onError(Context context, String arg1) {
		// HANDLE ERROR
		Log.v(TAG, "ERROR : NOT registered" + arg1);
		Toast.makeText(context, arg1, Toast.LENGTH_LONG).show();
		
	}

	@Override
	protected void onMessage(Context context, Intent arg1) {
		//LET'S BUILD A NOTIF
		//Toast.makeText(context, "notif received", Toast.LENGTH_LONG).show();
        Log.w(TAG, "eventid : " + arg1.getStringExtra("eventid"));
        Log.w(TAG, "action : " + arg1.getStringExtra("action"));
		if(arg1.getStringExtra("action") != null && arg1.getStringExtra("action").equals("warn_event")){
			NotifUtils.displayNotification(context, arg1.getStringExtra("message"), arg1.getStringExtra("title"),
					arg1.getStringExtra("eventid"), Integer.parseInt(arg1.getStringExtra("ttl")), arg1.getStringExtra("created_by"));
		} else if (arg1.getStringExtra("action") != null && arg1.getStringExtra("action").equals("start_countdown")){
			Intent intent = new Intent(Constant.BROADCAST_TAKE_PICTURE);
			intent.putExtra(TakePictureActivity.KEY_TAKEPICT_TIMESTAMP, arg1.getStringExtra("actiontime"));
			sendBroadcast(intent);
		} else if (arg1.getStringExtra("action") != null && arg1.getStringExtra("action").equals("new_user")){
            sendBroadcast(new Intent(Constant.BROADCAST_NEW_USER));
        } else if (arg1.getStringExtra("action") != null && arg1.getStringExtra("action").equals("photo_uploaded")){
            Log.w(TAG, "sendBroadcast(new Intent(Constant.BROADCAST_PICTURE_TAKEN));");
            sendBroadcast(new Intent(Constant.BROADCAST_PICTURE_TAKEN));
        }
		

		
	}

	@Override
	protected void onRegistered(Context context, String arg1) {
		//HERE WE NEED TO MAKE A CALL TO PUSHD
		String result = "SUCCESSFULLY registered" + GCMRegistrar.getRegistrationId(this);
		Log.i(TAG, result);
		NotificationHelper.getInstance().setApplicationContext(getApplicationContext());
		NotificationHelper.getInstance().startRegistrationProcess(null);
		
		
	}

	@Override
	protected void onUnregistered(Context arg0, String arg1) {
		//NOT HANDLED HERE
		
	}
	
	@Override
	protected String[] getSenderIds(Context context) {
		String[] ids = new String[1];
		ids[0] = Constant.GCM_SENDER_ID;
		return ids;
	}

}
